package game;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class GameManager {
    private static final GameManager instance = new GameManager();
    private  GameManager(){
    }
    public static GameManager getInstance(){
        return instance;
    }
    Background background;
    List<Catus> catusList= new CopyOnWriteArrayList<>();
    List <Cloud> cloudList = Collections.synchronizedList(new ArrayList<>());
    Pterosaur pterosaur;
    Trex trex;
    public ScoreBoard scoreBoard;
    GameSound gameSound;
    public void gameInit(){
        this.background = new Background();
        this.trex = new Trex();
        this.pterosaur = new Pterosaur();
        this.catusList.clear();
        this.cloudList.clear();
        GameData.gameState = 1;
        scoreBoard = new ScoreBoard();
        gameSound = new GameSound();

    }
    public void gameState(GameFrame gameFrame){
        while (true){
            if (GameData.gameState == 3){
                continue;
            }
            gameFrame.repaint();
            try {
                Thread.sleep(16);//1秒等于1000毫秒 60帧
                GameData.gameTime+=16;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            addCloud();
            addCatus();
        }//重调paint
    }
    private void addCloud(){
            if (GameData.cloudInterval>=1500){
                Cloud cloud = new Cloud();
                this.cloudList.add(cloud);
                GameData.cloudInterval = 0;
            }
    }
    private void addCatus(){
            if (GameData.catusInterval>=600){
                if (Math.random()*100>50){
                    Catus catus = new Catus();
                    if (pterosaur.x>20&&pterosaur.x<580){
                        this.catusList.add(catus);
                    }
                }
                GameData.catusInterval = 0;
            }
    }
    public void gameOver(){
        GameData.gameState = 3;
        GameManager.getInstance().gameSound.over.PlaySound();
    }

}
